#include "WString.h"
#include <sys/_intsup.h>
#include <sys/_stdint.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include "ui.h"

const char* ssid = "CMCC-vC2N";
const char* password = "1314520qq";
String de_time1 = "";
String de_time2 = "null";
String Status_code = "ok";

String my_token = "mP3ELuP966pUKmCz";//彩云天气token
String location_ip = "106.5916,30.1023";//要查询天气位置的经度,纬度


void tian_wifi_init (){
  int yyt = 0;
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED && yyt<=10) {
    delay(100);
    yyt++;
  }

}
void tian_wifi_end(){
 
      //WiFi.disconnect();//停止连接
      WiFi.mode(WIFI_OFF);//彻底关闭wifi的任何活动

}

void get_time(){   //苏宁易购的时间

// 创建 HTTPClient 对象
  HTTPClient http;

// 发送GET请求
  http.begin("http://quan.suning.com/getSysTime.do");

  // 获取响应状态码
  int httpCode = http.GET();


// 获取响应正文
String response = http.getString();//获取返回数据


 http.end();

  // 创建 DynamicJsonDocument 对象
  DynamicJsonDocument doc(1024);

  // 解析 JSON 数据
  deserializeJson(doc, response);

de_time1 = doc["sysTime1"].as<String>();
de_time2 = doc["sysTime2"].as<String>();

//de_time1 = de_Time1;//一串数字
//de_time2 = de_Time2;//不同单位间有恒杠


}


String sunrise_time = "";
String sunset_time = "";
String max_temp = "";
String min_temp = "";
String max_temp1 = "";
String min_temp1 = "";
String  max_temp2 = "";
String  min_temp2 = "";
String  weather_1 = "";
String  weather_2 = "";


void get_day_tianqi(){
// 创建 HTTPClient 对象
  HTTPClient http;

// 发送GET请求
  http.begin("https://api.caiyunapp.com/v2.6/"+my_token+"/"+location_ip+"/daily?dailysteps=3");

  // 获取响应状态码
  int httpCode = http.GET();


    if (httpCode != HTTP_CODE_OK) // 请求被服务器正常响应，等同于httpCode == 200
    {
      httpCode = http.GET();
    }

// 获取响应正文
      String response = http.getString();//获取返回数据

 http.end();

  // 创建 DynamicJsonDocument 对象
  DynamicJsonDocument doc1(4*1024);

  // 解析 JSON 数据
  deserializeJson(doc1, response);
  
  sunrise_time = doc1["result"]["daily"]["astro"][0]["sunrise"]["time"].as<String>();
  sunset_time =  doc1["result"]["daily"]["astro"][0]["sunset"]["time"].as<String>();
  max_temp  =  doc1["result"]["daily"]["temperature"][0]["max"].as<String>();
  min_temp = doc1["result"]["daily"]["temperature"][0]["min"].as<String>();
  max_temp1  =  doc1["result"]["daily"]["temperature"][1]["max"].as<String>();
  min_temp1 = doc1["result"]["daily"]["temperature"][1]["min"].as<String>();
  max_temp2  =  doc1["result"]["daily"]["temperature"][2]["max"].as<String>();
  min_temp2 = doc1["result"]["daily"]["temperature"][2]["min"].as<String>();
  weather_1 = doc1["result"]["daily"][ "skycon"][1]["value"].as<String>();
  weather_2 = doc1["result"]["daily"][ "skycon"][2]["value"].as<String>();

  Status_code = doc1["status"].as<String>();
  
    if(weather_1=="CLEAR_DAY") {weather_1 = "晴";}  
    if(weather_1=="CLEAR_NIGHT"){weather_1 = "晴";}  
    if(weather_1=="PARTLY_CLOUDY_DAY") {weather_1 = "多云";}
    if(weather_1=="PARTLY_CLOUDY_NIGHT") {weather_1 = "多云";}
    if(weather_1=="CLOUDY") {weather_1 = "阴";}
    if(weather_1=="LIGHT_HAZE") {weather_1 = "轻度雾霾";}
    if(weather_1=="MODERATE_HAZE") {weather_1 = "中度雾霾";}
    if(weather_1=="HEAVY_HAZE") {weather_1 = "重度雾霾";}
    if(weather_1=="LIGHT_RAIN") {weather_1 = "小雨";}
    if(weather_1=="MODERATE_RAIN") {weather_1 = "中雨";}
    if(weather_1=="HEAVY_RAIN") {weather_1 = "大雨";}
    if(weather_1=="STORM_RAIN") {weather_1 = "暴雨";}
    if(weather_1=="FOG") {weather_1 = "雾";}
    if(weather_1=="LIGHT_SNOW") {weather_1 = "小雪";}
    if(weather_1=="MODERATE_SNOW") {weather_1 ="中雪";}
    if(weather_1=="HEAVY_SNOW") {weather_1 = "大雪";}
    if(weather_1=="STORM_SNOW") {weather_1 = "暴雪";}
    if(weather_1=="DUST") {weather_1 = "浮尘";}
    if(weather_1=="SAND") {weather_1 = "沙尘";}
    if(weather_1=="WIND") {weather_1 = "大风";}

    
  

    if(weather_2=="CLEAR_DAY") {weather_2 = "晴";}  
    if(weather_2=="CLEAR_NIGHT"){weather_2 = "晴";}  
    if(weather_2=="PARTLY_CLOUDY_DAY") {weather_2 = "多云";}
    if(weather_2=="PARTLY_CLOUDY_NIGHT") {weather_2 = "多云";}
    if(weather_2=="CLOUDY") {weather_2 = "阴";}
    if(weather_2=="LIGHT_HAZE") {weather_2 = "轻度雾霾";}
    if(weather_2=="MODERATE_HAZE") {weather_2 = "中度雾霾";}
    if(weather_2=="HEAVY_HAZE") {weather_2 = "重度雾霾";}
    if(weather_2=="LIGHT_RAIN") {weather_2 = "小雨";}
    if(weather_2=="MODERATE_RAIN") {weather_2 = "中雨";}
    if(weather_2=="HEAVY_RAIN") {weather_2 = "大雨";}
    if(weather_2=="STORM_RAIN") {weather_2 = "暴雨";}
    if(weather_2=="FOG") {weather_2 = "雾";}
    if(weather_2=="LIGHT_SNOW") {weather_2 = "小雪";}
    if(weather_2=="MODERATE_SNOW") {weather_2 ="中雪";}
    if(weather_2=="HEAVY_SNOW") {weather_2 = "大雪";}
    if(weather_2=="STORM_SNOW") {weather_2 = "暴雪";}
    if(weather_2=="DUST") {weather_2 = "浮尘";}
    if(weather_2=="SAND") {weather_2 = "沙尘";}
    if(weather_2=="WIND") {weather_2 = "大风";}


if (Status_code == "ok"){
    _ui_label_set_property(ui_Label6,_UI_LABEL_PROPERTY_TEXT,max_temp.c_str());//今天最高温度
    _ui_label_set_property(ui_Label2,_UI_LABEL_PROPERTY_TEXT,String("/"+min_temp).c_str());//今天最低温度
    _ui_label_set_property(ui_Label7,_UI_LABEL_PROPERTY_TEXT,String("明天"+weather_1+max_temp1+"/"+min_temp1).c_str());//明天天气预报
    _ui_label_set_property(ui_Label8,_UI_LABEL_PROPERTY_TEXT,String("后天"+weather_2+max_temp2+"/"+min_temp2).c_str());//后天天气预报
    _ui_label_set_property(ui_Label9,_UI_LABEL_PROPERTY_TEXT,sunrise_time.c_str());//日升
    _ui_label_set_property(ui_Label10,_UI_LABEL_PROPERTY_TEXT,sunset_time.c_str());//日落
}


  



}


String tg_temp = "";
String weather = "";
String rain_gl = "";
void get_hour_tianqi(){
// 创建 HTTPClient 对象
  HTTPClient http;

// 发送GET请求
  http.begin("https://api.caiyunapp.com/v2.6/"+my_token+"/"+location_ip+"/hourly?hourlysteps=1");

  // 获取响应状态码
  int httpCode = http.GET();

 if (httpCode != HTTP_CODE_OK) // 请求被服务器正常响应，等同于httpCode == 200
    {
      httpCode = http.GET();
    }

  // 获取响应正文
  String response = http.getString();

 http.end();

  // 创建 DynamicJsonDocument 对象
  DynamicJsonDocument doc2(1024);

  // 解析 JSON 数据
  deserializeJson(doc2, response);

  tg_temp = doc2["result"]["hourly"][ "apparent_temperature"][0]["value"].as<String>();
  weather = doc2["result"]["hourly"]["skycon"][0]["value"].as<String>();
  Status_code = doc2["status"].as<String>();
  rain_gl = doc2["result"]["hourly"]["precipitation"][0]["probability"].as<String>();

    if(weather=="CLEAR_DAY") {weather = "晴"; lv_image_set_src(ui_Image1, &ui_img_black_clear_day_png); }  
    if(weather=="CLEAR_NIGHT"){weather = "晴";lv_image_set_src(ui_Image1, &ui_img_black_clear_night_png);}  
    if(weather=="PARTLY_CLOUDY_DAY") {weather = "多云";lv_image_set_src(ui_Image1, &ui_img_black_partly_cloudy_day_png);}
    if(weather=="PARTLY_CLOUDY_NIGHT") {weather = "多云";lv_image_set_src(ui_Image1, &ui_img_black_partly_cloudy_night_png);}
    if(weather=="CLOUDY") {weather = "阴";lv_image_set_src(ui_Image1, &ui_img_black_cloudy_png);}
    if(weather=="LIGHT_HAZE") {weather = "轻度雾霾";lv_image_set_src(ui_Image1, &ui_img_black_light_haze_png);}
    if(weather=="MODERATE_HAZE") {weather = "中度雾霾";lv_image_set_src(ui_Image1, &ui_img_black_light_haze_png);}
    if(weather=="HEAVY_HAZE") {weather = "重度雾霾";lv_image_set_src(ui_Image1, &ui_img_black_light_haze_png);}
    if(weather=="LIGHT_RAIN") {weather = "小雨";lv_image_set_src(ui_Image1, &ui_img_black_light_rain_png);}
    if(weather=="MODERATE_RAIN") {weather = "中雨";lv_image_set_src(ui_Image1, &ui_img_black_moderate_rain_png);}
    if(weather=="HEAVY_RAIN") {weather = "大雨";lv_image_set_src(ui_Image1, &ui_img_black_heavy_rain_png);}
    if(weather=="STORM_RAIN") {weather = "暴雨";lv_image_set_src(ui_Image1, &ui_img_black_storm_rain_png);}
    if(weather=="FOG") {weather = "雾";lv_image_set_src(ui_Image1, &ui_img_black_fog_png);}
    if(weather=="LIGHT_SNOW") {weather = "小雪";lv_image_set_src(ui_Image1, &ui_img_black_light_snow_png);}
    if(weather=="MODERATE_SNOW") {weather ="中雪";lv_image_set_src(ui_Image1, &ui_img_black_moderate_snow_png);}
    if(weather=="HEAVY_SNOW") {weather = "大雪";lv_image_set_src(ui_Image1, &ui_img_black_heavy_snow_png);}
    if(weather=="STORM_SNOW") {weather = "暴雪";lv_image_set_src(ui_Image1, &ui_img_black_storm_snow_png);}
    if(weather=="DUST") {weather = "浮尘";lv_image_set_src(ui_Image1, &ui_img_black_dust_png);}
    if(weather=="SAND") {weather = "沙尘";lv_image_set_src(ui_Image1, &ui_img_black_sand_png);}
    if(weather=="WIND") {weather = "大风";lv_image_set_src(ui_Image1, &ui_img_black_wind_png);}
  

  if(Status_code == "ok"){
    _ui_label_set_property(ui_Label1,_UI_LABEL_PROPERTY_TEXT,weather.c_str());//今天天气
    _ui_label_set_property(ui_Label14,_UI_LABEL_PROPERTY_TEXT,String("未来一小时降水概率: %"+rain_gl).c_str());
    _ui_label_set_property(ui_Label43,_UI_LABEL_PROPERTY_TEXT,String("体感"+tg_temp+"度").c_str());//体感温度
  } 


}



 
















